package com.xiaoh.compare.utils;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * 读取文件，按行来读取
 */
public class CompareCrosswise {
    private static List<String> fileToLines(String filename) {
        List<String> lines = new LinkedList<String>();
        String line = "";
        try {
            //编码为UTF8的情况
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF8"));
            //编码为GBK的情况
//	         BufferedReader in  =new BufferedReader(new InputStreamReader(new FileInputStream(filename),"GBK"));
            //编码默认为UTF8的情况
//			 BufferedReader in = new BufferedReader(new FileReader(filename));
            while ((line = in.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    /**
     * 功能描述 对比每行的重复子串并合并
     * @author
     * @date 2020/5/8
     * @param [str, str2]
     * @return java.lang.String
     */
    public static String compareString(String str, String str2) {
        char[] ary = str.toCharArray();
        char[] ary2 = str2.toCharArray();
        int aryLen = ary.length;
        int ary2Len = ary2.length;
        int[][] temp = new int[ary.length][ary2.length];    //声明一个二维数组，存储最长公共子串长度

        for (int i = 0; i < ary.length; i++) {
            for (int j = 0; j < ary2.length; j++) {
                if (ary[i] == ary2[j]) temp[i][j] = 1;
                else temp[i][j] = 0;
            }
        }

        int len = 0;
        int begin = aryLen;
        int end = 0;
        for (int i = ary2.length - 1; i >= 0; i--) {
            if (temp[ary.length - 1][i] == 1) {
                if (temp[ary.length - 1 - i][0] == 1) {
                    begin = ary.length - 1 - i;
                    len = i + 1;
                    end = ary.length - 1;
                    break;
                }
            }
        }
        String str1 = "";
        for (int i = 0; i < begin; i++) {
            str1 += ary[i];
        }
        String s = str1.concat(str2);
        return s;
    }

    //对比两个文件的内容（横向）并合并文件
    public static void compareFile(String url1, String url2, String outputUrl) throws IOException {
        BufferedReader url1Input = new BufferedReader(new FileReader(url1));
        BufferedReader url2Input = new BufferedReader(new FileReader(url2));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputUrl));

        FileOutputStream fos = null;
        fos = new FileOutputStream(outputUrl);

        String str1 = " ";
        String str2 = " ";
        while ((str1 = url1Input.readLine()) != null && (str2 = url2Input.readLine()) != null) {

            String s = compareString(str1, str2);
            bufferedWriter.write(s + "\n");
            System.out.println("s:" + s);
        }

        System.out.println("写入结束！");

        bufferedWriter.flush();
        bufferedWriter.close();
        url1Input.close();
        url2Input.close();

    }

    public static String compareStringOne(String string1, String string2) {
        String[] strs = string1.split("\n");
        String[] strs2 = string2.split("\n");
        String s = "";
        for (int i = 0; i < strs.length; i++) {
            s += compareString(strs[i], strs2[i]) + "\n";

        }
        return s;
    }

    //编译环境为GBK的情况
    public static void compareFileGBK(String url1, String url2, String outputUrl) throws IOException {
        BufferedReader url1Input = new BufferedReader(new InputStreamReader(new FileInputStream(url1), "GBK"));
        BufferedReader url2Input = new BufferedReader(new InputStreamReader(new FileInputStream(url2), "GBK"));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputUrl), "GBK"));

        String str1 = " ";
        String str2 = " ";
        while ((str1 = url1Input.readLine()) != null && (str2 = url2Input.readLine()) != null) {
            String s = compareString(str1, str2);
            System.out.println(s);
            bufferedWriter.write(s + "\r");
            bufferedWriter.write("\n");
        }

        System.out.println("写入结束！");

        bufferedWriter.flush();
        bufferedWriter.close();
        url1Input.close();
        url2Input.close();

    }

    //编译环境为utf8的情况
    public static void compareFileGBK2(String url1, String url2, String outputUrl) throws IOException {
        BufferedReader url1Input = new BufferedReader(new InputStreamReader(new FileInputStream(url1), "utf8"));
        BufferedReader url2Input = new BufferedReader(new InputStreamReader(new FileInputStream(url2), "utf8"));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputUrl), "utf8"));
        String str1 = " ";
        String str2 = " ";
        while ((str1 = url1Input.readLine()) != null && (str2 = url2Input.readLine()) != null) {
            String s = compareString(str1, str2);
            System.out.println(s);
            //每一行写入文件后给其换行
            bufferedWriter.write(s + "\r");
            bufferedWriter.write("\n");
        }

        System.out.println("写入结束！");

        bufferedWriter.flush();
        bufferedWriter.close();
        url1Input.close();
        url2Input.close();

    }

    public static void compareFileUTF8(String url1, String url2, String outputUrl) throws IOException {
        BufferedReader url1Input = new BufferedReader(new InputStreamReader(new FileInputStream(url1), "utf8"));
        BufferedReader url2Input = new BufferedReader(new InputStreamReader(new FileInputStream(url2), "utf8"));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputUrl), "utf8"));
        String str1 = " ";
        String str2 = " ";
        while ((str1 = url1Input.readLine()) != null && (str2 = url2Input.readLine()) != null) {
            String s = compareString(str1, str2);
            System.out.println(s);
            //每一行写入文件后给其换行
            bufferedWriter.write(s + "\r");
            bufferedWriter.write("\n");
        }

        System.out.println("写入结束！");

        bufferedWriter.flush();
        bufferedWriter.close();
        url1Input.close();
        url2Input.close();
    }


    public static void main(String[] args) throws IOException {

        //对每行文本进行比对
        String str1 = "姓名：张三 性别：男 年龄：24 出生日期：1991-10-02";
        String str2 = "姓名：李四 性别：男 年龄：24 出生日期：1991-10-02 测试";
        String str4 = "asdadsadadasfsdfsd";
        String str5 = "出生日期：1991-10-02 测试 asdadsadadasfsdfsd";
        String str7 = "性别：男 年龄：24";
        String str3 = CompareCrosswise.compareString(str1, str4);
        String str6 = CompareCrosswise.compareString(str3, str5);
        System.out.println("针对文本每行进行比对：" + str3);
        System.out.println("针对文本每行进行比对：" + str6);
        System.out.println("针对文本每行进行比对：" + compareString(str2, str5));
        System.out.println("针对文本每行进行比对：" + compareString(str2, str5));
        System.out.println("针对文本每行进行比对：" + compareString(str2, str7));



//        String str1 = "/Users/xiaoh/Desktop/09-OCR/test/aa.txt";
//        String str2 = "/Users/xiaoh/Desktop/09-OCR/test/bb.txt";
//        String str3 = "/Users/xiaoh/Desktop/09-OCR/test/dd.txt";
//
//        CompareCrosswise.compareFileGBK2(str1, str2, str3);
//        System.out.println("success");
    }
}
